﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Motoball.screenmanager;

namespace Motoball.screens
{
    public class CountdownScreen : SplashScreen
    {
        /// <summary>
        /// 4 models - 0==3, 1==2, 2==1, 3==Go!
        /// </summary>
        BasicModel[] countdownModels;
        int currentModel = 3;
        public bool active = true;

        TimeSpan timer;

        public CountdownScreen(string screenName)
            : base(screenName)
        {  
        }

        #region Methods
        public override void Reset()
        {
            base.Reset();
            active = true;
        }
        public override void LoadContent()
        {
            base.LoadContent();
            ContentManager content = screenManager.Content;

            countdownModels = new BasicModel[4];
            //countdownModels[0] = new BasicModel(content.Load<Model>("Models\\cd3"));
            //countdownModels[1] = new BasicModel(content.Load<Model>("Models\\cd2"));
            //countdownModels[2] = new BasicModel(content.Load<Model>("Models\\cd1"));
            //countdownModels[3] = new BasicModel(content.Load<Model>("Models\\go"));
            countdownModels[0] = new BasicModel(content.Load<Model>("Models\\3"));
            countdownModels[1] = new BasicModel(content.Load<Model>("Models\\2"));
            countdownModels[2] = new BasicModel(content.Load<Model>("Models\\1"));
            countdownModels[3] = new BasicModel(content.Load<Model>("Models\\Go"));
            
           
            //for (int i = 0; i < 4; i++)
            //{
            //    foreach (ModelMesh mesh in countdownModels[i].model.Meshes)
            //    {
            //        countdownModels[i].diffColors[mesh.Name] = new Vector3(0.6f + i/8f);
            //        BasicEffect be = (BasicEffect)mesh.Effects[0];
            //        be.DiffuseColor = countdownModels[i].diffColors[mesh.Name];
            //    }                
            //}

            countdownModels[3].World = Matrix.CreateScale(0.6f);
            countdownModels[3].World *= Matrix.CreateRotationX(MathHelper.PiOver2);
            for (int i = 0; i < 4; i++)
            {
                countdownModels[i].World *= Matrix.CreateRotationZ(MathHelper.Pi);
                countdownModels[i].World *= Matrix.CreateTranslation(0, Camera.gameplayCamDistance-100, -5);
                countdownModels[i].drawSetId = "foreground";
            }
            


        }


        public override void Update(GameTime gameTime)
        {
            if (!active)
                return;
            timer += gameTime.ElapsedGameTime;
            switch (timer.Seconds)
            {
                case 0:
                    break;
                //3
                case 1:
                    if (currentModel != 0)
                    {
                        GameplayScreen gameplay = (GameplayScreen)screenManager.GetScreen("gameplayScreen");
                        gameplay.ResetPositions();
                        currentModel = 0;
                        AudioManager.PlayCue("cd1");
                    }
                    break;
                //2
                case 2:
                    if (currentModel != 1)
                    {
                        currentModel = 1;
                        AudioManager.PlayCue("cd1");
                    }
                    break;
                    //1
                case 3:
                    if (currentModel != 2)
                    {
                        currentModel = 2;
                        AudioManager.PlayCue("cd1");
                    }
                    break;
                    //GO!
                case 4: 
                //start game after 3.5 seconds
                    if (timer.Milliseconds > 3500)
                        Exit();
                    else if (currentModel != 3)
                    {
                        currentModel = 3;
                        AudioManager.PlayCue("cd2");
                    }
                    break;
                default:
                    Exit();
                    break;
            }
                
        }

        public override void Draw(GameTime gameTime)
        {
            if (timer.Seconds > 0)
            {
                base.Draw(gameTime);
                countdownModels[currentModel].Draw(Camera);
            }
        }

        public void StartCountdown(TimeSpan timer)
        {
            active = true;
            this.timer = timer;
            screenManager.AddScreen(this);
        }
        protected override void Exit()
        {
            GameplayScreen gameplayScreen = (GameplayScreen)screenManager.GetScreen("gameplayScreen");
            gameplayScreen.active = GameplayScreen.Activity.Active;
            screenManager.RemoveScreen(this);
        }

        #endregion

        #region Properties
        public TimeSpan Timer
        {
            get { return timer; }
            set { timer = value; }
        }
        #endregion

    }
}
